package com.workcell.workdata.xthings.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workcell.workdata.xthings.constant.HardwareStateEnum;
import com.workcell.workdata.xthings.entity.BaseVoiceSynthesis;
import com.workcell.workdata.xthings.entity.CodeChainnum;
import com.workcell.workdata.xthings.entity.CodeVoiceSynthesis;
import com.workcell.workdata.xthings.mapper.CodeVoiceSynthesisMapper;
import com.workcell.workdata.xthings.service.BaseVoiceSynthesisService;
import com.workcell.workdata.xthings.service.CodeVoiceSynthesisService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Admin
 * @description 针对表【t_code_voice_synthesis(语音合成播放信息表)】的数据库操作Service实现
 * @createDate 2023-08-03 09:17:40
 */
@Service
public class CodeVoiceSynthesisServiceImpl extends ServiceImpl<CodeVoiceSynthesisMapper, CodeVoiceSynthesis>
        implements CodeVoiceSynthesisService {

    @Resource
    private BaseVoiceSynthesisService baseVoiceSynthesisService;

    /**
     * 获取硬件基本参数信息
     *
     * @param baseIds
     * @return
     */
    @Override
    public Object getHardwareBase(String baseIds) {
        List<String> baseList = Arrays.asList(baseIds.split(","));
        LambdaQueryWrapper<BaseVoiceSynthesis> baseVoiceSynthesisLambdaQueryWrapper = new QueryWrapper<BaseVoiceSynthesis>().lambda()
                .in(CollectionUtil.isNotEmpty(baseList), BaseVoiceSynthesis::getId, baseList);
        return baseVoiceSynthesisService.list(baseVoiceSynthesisLambdaQueryWrapper);
    }

    /**
     * 保存二维码基本快照信息
     *
     * @param codeChainnum
     * @return
     */
    @Override
    public Boolean saveCodeBaseInfo(CodeChainnum codeChainnum) {
        // 查询硬件信息
        LambdaQueryWrapper<BaseVoiceSynthesis> baseCondition = new QueryWrapper<BaseVoiceSynthesis>().lambda()
                .eq(BaseVoiceSynthesis::getId, codeChainnum.getBaseId());
        BaseVoiceSynthesis baseVoiceSynthesis = baseVoiceSynthesisService.getOne(baseCondition);
        if (ObjectUtil.isEmpty(baseVoiceSynthesis)) {
            return false;
        }
        // 添加二维码快照信息
        CodeVoiceSynthesis saveBean = new CodeVoiceSynthesis();
        BeanUtil.copyProperties(baseVoiceSynthesis, saveBean, "createBy", "createTime", "updateBy", "updateTime");
        saveBean.setCodeId(codeChainnum.getCodeId());
        saveBean.setChainNum(codeChainnum.getChainNum());
        saveBean.setDeviceNo(codeChainnum.getDeviceNo());
        return this.save(saveBean);
    }


    /**
     * 获取硬件清单
     *
     * @return
     */
    @Override
    public List<JSONObject> getHardwareList() {
        LambdaQueryWrapper<BaseVoiceSynthesis> lambdaQueryWrapper = Wrappers.lambdaQuery(BaseVoiceSynthesis.class);
        lambdaQueryWrapper.eq(BaseVoiceSynthesis::getState, HardwareStateEnum.INUSE.getCode());
        return baseVoiceSynthesisService.list(lambdaQueryWrapper).stream().map(i -> {
            JSONObject resultBean = new JSONObject();
            resultBean.put("baseId", i.getId());
            resultBean.put("manufacturer", i.getManufacturer());
            resultBean.put("hardwareName", i.getName());
            return resultBean;
        }).collect(Collectors.toList());
    }


    /**
     * 查询硬件快照信息
     *
     * @param codeId
     * @return
     */
    @Override
    public Object getHardwareInfo(String codeId) {
        return this.getById(codeId);
    }

    @Override
    public Object getHardwareBaseOpen(String deviceNo) {
        LambdaQueryWrapper<CodeVoiceSynthesis> lambdaQueryWrapper = Wrappers.lambdaQuery(CodeVoiceSynthesis.class);
        lambdaQueryWrapper.eq(CodeVoiceSynthesis::getDeviceNo, deviceNo);
        CodeVoiceSynthesis codeVoiceSynthesis = this.getOne(lambdaQueryWrapper);
        return codeVoiceSynthesis;
    }
}




